package simi;

import java.io.File;
import java.io.FilenameFilter;
import java.util.StringTokenizer;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.lang.StringUtils;

public class SimiUtils {

	private static byte[] B1 = { 27, 91, 48, 109 };
	private static byte[] B2 = { 27, 91, 49, 109 };
	private static String FILTER_1 = new String(B1);
	private static String FILTER_2 = new String(B2);
	private static final String NEW_LINE = "\n";

	private SimiUtils() {
	}

	public static String getFileExtension(String filename) {

		String ext = null;
		int dotPlace = filename.lastIndexOf('.');

		if (dotPlace >= 0) {
			ext = filename.substring(dotPlace + 1);
		}

		return ext;
	}

	public static File[] listFiles(File directory, FilenameFilter filter,
			boolean recurse) {

		Collection<File> files = listFileCollection(directory, filter, recurse);
		File[] arr = new File[files.size()];
		return files.toArray(arr);
	}

	public static Collection<File> listFileCollection(File directory,
			FilenameFilter filter, boolean recurse) {

		Collection<File> files = new ArrayList<File>();
		File[] entries = directory.listFiles();

		for (File entry : entries) {
			if (filter == null || filter.accept(directory, entry.getName())) {
				files.add(entry);
			}

			// If the file is a directory and the recurse flag
			// is set, recurse into the directory
			if (recurse && entry.isDirectory()) {
				files.addAll(listFileCollection(entry, filter, recurse));
			}
		}

		// Return collection of files
		return files;
	}

	public static File getLastModified(File[] theFiles) {

		File theLastModifiedFile = null;
		if (theFiles != null) {
			for (int i = 0; i < theFiles.length; i++) {
				File theFile = theFiles[i];
				if (theLastModifiedFile == null
						|| theFile.lastModified() > theLastModifiedFile
								.lastModified())
					theLastModifiedFile = theFile;
			}
		}

		return theLastModifiedFile;
	}

	public static String fixRInput(String inputStr) {

		if (inputStr == null) {
			inputStr = Constants.NEWLINE;
		} else if (!inputStr.endsWith(Constants.NEWLINE)) {
			inputStr = inputStr + Constants.NEWLINE;
		}

		return inputStr;
	}

	public static String fixROutput(String theString) {
		String[] codes = SimiPropertyManager.getConfig().getStringArray("sessionpage.filter");
		for (String code: codes)theString =  theString.replaceAll(code,"");
		return theString;
	}

	public static List<String> splitInChunks(String s){
		List<String> result = new ArrayList<String>();
   		StringTokenizer st1 = new StringTokenizer(s, NEW_LINE);
    		while(st1.hasMoreTokens()){
			String tmp = st1.nextToken();
			// This regexp trims trailing spaces
			tmp = tmp.replaceAll("\\s+$", "");
			if(tmp.trim().length() > 0)
				result.add(tmp + NEW_LINE);
		}
		// This allows the user to enter a newline at the command line
		if(result.size() == 0)
			result.add(NEW_LINE);
		return result;
	}
}
